package com.sky.utils;

import com.sky.properties.MinioProperties;
import io.minio.MinioClient;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class MinioUtil {
    private final MinioProperties minioProperties;

    // 构造函数，用于初始化MinioUtil对象
    @Autowired
    public MinioUtil(MinioProperties minioProperties) {
        // 将传入的MinioProperties对象赋值给成员变量
        this.minioProperties = minioProperties;
    }

    @Bean
    // 创建一个MinioClient的Bean
    public MinioClient minioClient() {
        // 使用MinioClient.builder()创建一个MinioClient的构建器
        return MinioClient.builder()
                // 设置Minio的Endpoint
                .endpoint(minioProperties.getEndpoint())
                // 设置Minio的AccessKey和SecretKey
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                // 设置OkHttpClient的连接超时时间和写入超时时间
                .httpClient(new OkHttpClient.Builder()
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .writeTimeout(60, TimeUnit.SECONDS)
                        .build())
                // 构建MinioClient
                .build();
    }


}
